package com.tbm.console.proj.json;

import com.tbm.console.base.controller.BaseController;
import com.tbm.console.proj.entity.LocalConstant;
import com.tbm.console.proj.service.LocalConstantService;
import com.tbm.console.util.DateUtil;
import com.tbm.console.util.PageData;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

@RestController
public class LocalConstantJson extends BaseController{

    @Autowired
    private LocalConstantService localConstantService;

    /**
     * 显示list
     * @param page
     * @param size
     * @return
     */
    @RequestMapping(value = "/constant-list/{page}/{size}")
    public Map<String,Object> localConstantList(@PathVariable("page") Integer page, @PathVariable("size") Integer size){
        PageData pageData = this.getPageData();
        pageData.put("deleteFlag", "N");
        long count = localConstantService.count(pageData);
        List<LocalConstant> localConstants = localConstantService.findAll(pageData,page-1,size);
        pageData.put("total",count);
        pageData.put("rows", localConstants);
        return pageData;
    }

    /**
     * 常量增删改
     * @return
     */
    @RequestMapping(value = "/constant-ins-upd-del")
    public Object logCreateUpdate(){
        PageData pageData = this.getPageData();
        Integer id = -1;
        if(null != pageData.get("id")&&!"".equals(pageData.get("id").toString())){
            id = Integer.valueOf(pageData.get("id").toString());
        }
        LocalConstant localConstant = localConstantService.findById(id);

        //------------------------------------------------------- 新增修改操作 ----------------------------------------------------------------------//

        if(-1==id){
            localConstant.setCreateTime(DateUtil.getTime());
        }else{
            localConstant.setUpdateTime(DateUtil.getTime());
        }

        //字段信息维护
        if(null!=pageData.get("constantKey")&&!"".equals(pageData.get("constantKey").toString())){
            localConstant.setConstantKey(pageData.get("constantKey").toString());
        }
        //字段信息维护
        if(null!=pageData.get("constantKeyDesc")&&!"".equals(pageData.get("constantKeyDesc").toString())){
            localConstant.setConstantKeyDesc(pageData.get("constantKeyDesc").toString());
        }
        //字段信息维护
        if(null!=pageData.get("constantValue")&&!"".equals(pageData.get("constantValue").toString())){
            localConstant.setConstantValue(pageData.get("constantValue").toString());
        }
        //字段信息维护
        if(null!=pageData.get("constantValueDesc")&&!"".equals(pageData.get("constantValueDesc").toString())){
            localConstant.setConstantValueDesc(pageData.get("constantValueDesc").toString());
        }
        //------------------------------------------------------- 删除操作 ---------------------------------------------------------------------------//
        if(null!=pageData.get("flag")&&"Y".equals(pageData.get("flag").toString())){
            localConstant.setDeleteFlag(pageData.get("flag").toString());
        }
        localConstantService.save(localConstant);
        return null;
    }


    /**
     * 通过常量类型，来获取常量列表
     * @param type
     * @return
     */
    @RequestMapping(value = "/constants/{type}")
    public Object constantsByType(@PathVariable("type") String type){
        //先查redis，如果缓存有，直接返回，如果没有查询数据库，在将查询出来的结果存储到缓存中
        List<PageData> pageList = new ArrayList<>();
        PageData pageData = new PageData();
        pageData.put("constantKey", type);
        List<LocalConstant> all = localConstantService.findAll(pageData);
        for(LocalConstant localConstant : all){
            PageData pd = new PageData();
            pd.put("value",localConstant.getConstantValueDesc());
            pd.put("key",localConstant.getConstantValue());
            pageList.add(pd);
        }
        return pageList;
    }
}
